package com.omega.controller;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.omega.common.utils.Utils;
import com.omega.entity.ItemReceived;
import com.omega.service.ItemDenominationService;
import com.omega.service.ItemReceivedService;
import com.omega.service.ItemService;
import com.omega.service.MeasuringUnitService;
import com.omega.service.SupplierService;
import com.omega.vo.ItemReceivedList;

@Controller
@RequestMapping(value = "/user/itemReceived")
public class ItemReceivedController {

	@Autowired
	ItemReceivedService itemReceivedService;

	@Autowired
	ItemService itemService;

	@Autowired
	ItemDenominationService itemDenominationService;

	@Autowired
	ItemReceivedList itemReceivedList;

	@Autowired
	MeasuringUnitService measuringUnitService;

	@Autowired
	SupplierService supplierService;

	@RequestMapping(value = "/addItemReceived", method = RequestMethod.GET)
	public String showAddItemReceivedForm(Model model) {
		List<ItemReceived> itemInputList = new CopyOnWriteArrayList<ItemReceived>();
		for (int i = 0; i < 5; i++) {
			itemInputList.add(new ItemReceived());
		}
		itemReceivedList.setItemReceivedList(itemInputList);
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		model.addAttribute("itemsList", itemService.loadAllItems());
		model.addAttribute("denominationsList",
				itemDenominationService.loadAllDenominations());
		model.addAttribute("measuringUnitsList",
				measuringUnitService.loadAllMeasuringUnits());
		model.addAttribute("suppliersList", supplierService.loadAllSuppliers());
		model.addAttribute("itemReceivedListForm", itemReceivedList);
		return "addItemReceived";
	}

	@RequestMapping(value = "/addItemReceived", method = RequestMethod.POST)
	public String submitAddItemReceivedForm(
			@ModelAttribute("itemReceivedListForm") ItemReceivedList itemReceivedListForm,
			BindingResult result, Model model, HttpServletRequest request) {
		for (ItemReceived form : itemReceivedListForm.getItemReceivedList()) {
			if (!(StringUtils.isEmpty(form.getBillNo()))
					& !(StringUtils.isEmpty(form.getItem()))) {
				itemReceivedService.addReceiveditem(form);
			}
		}
		return "redirect:/user/itemReceived/viewItemsReceived";
	}

	@RequestMapping(value = "/editItemReceived/{itemReceivedId}", method = RequestMethod.GET)
	public String showEditItemReceivedForm(@PathVariable String itemReceivedId,
			Model model) {
		ItemReceived itemReceived = itemReceivedService.loadById(Long
				.parseLong(itemReceivedId));
		model.addAttribute("itemReceivedForm", itemReceived);
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		model.addAttribute("itemsList", itemService.loadAllItems());
		model.addAttribute("denominationsList",
				itemDenominationService.loadAllDenominations());
		model.addAttribute("measuringUnitsList",
				measuringUnitService.loadAllMeasuringUnits());
		model.addAttribute("suppliersList", supplierService.loadAllSuppliers());
		return "editItemReceived";
	}

	@RequestMapping(value = "/editItemReceived/{itemReceivedId}", method = RequestMethod.POST)
	public String submitEditItemReceivedForm(
			@ModelAttribute("itemReceivedForm") ItemReceived form,
			BindingResult result, Model model, HttpServletRequest request) {
		itemReceivedService.updateItemReceived(form);
		return "redirect:/user/itemReceived/viewItemsReceived";
	}

	@RequestMapping(value = "/viewItemsReceived", method = RequestMethod.GET)
	public String viewAllItemsReceived(Model model) {
		model.addAttribute("itemReceivedList",
				itemReceivedService.loadAllReceivedItems());
		return "viewItemsReceived";
	}
	
	@RequestMapping(value = "/deleteItemReceived/{itemReceivedId}", method = RequestMethod.GET)
	public String deleteItemReceived(@PathVariable("itemReceivedId") String itemReceivedId, HttpServletRequest request) {
		itemReceivedService.deleteItemReceived(Long.parseLong(itemReceivedId));
		return "redirect:/user/itemReceived/viewItemsReceived";
	}

}
